package com.tencent.yumeko;

import com.google.common.collect.Maps;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@RequestMapping("/consumer")
public class MetadataConsumerController {

    private final RestTemplate restTemplate;

    private final MetadataConsumerService metadataConsumerService;

    private final MetadataLocalProperties metadataLocalProperties;

    public MetadataConsumerController(RestTemplate restTemplate,
                                      MetadataConsumerService metadataConsumerService,
                                      MetadataLocalProperties metadataLocalProperties) {
        this.restTemplate = restTemplate;
        this.metadataConsumerService = metadataConsumerService;
        this.metadataLocalProperties = metadataLocalProperties;
    }

    /**
     * Get metadata info from remote service.
     *
     * @return metadata map
     */
    @GetMapping("/feign/info")
    public Map<String, Map<String, String>> feign() {
        Map<String, Map<String, String>> ret = Maps.newHashMap();

        // Call remote service with feign client
        Map<String, String> producerMetadata = metadataConsumerService.info();
        ret.put("producer-transitive-metadata", producerMetadata);

        // Get Custom Metadata From Context
        MetadataContext context = MetadataContextHolder.get();
        Map<String, String> consumerTransitiveMetadata = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
        ret.put("consumer-transitive-metadata", consumerTransitiveMetadata);
        ret.put("consumer-metadata-contents", metadataLocalProperties.getContent());

        return ret;
    }

    /**
     * Get metadata information of callee.
     *
     * @return information of callee
     */
    @SuppressWarnings("unchecked")
    @GetMapping("/rest/info")
    public Map<String, Map<String, String>> rest() {
        Map<String, Map<String, String>> ret = Maps.newHashMap();

        // Call remote service with RestTemplate
        Map<String, String> producerMetadata = restTemplate.getForObject(
                "http://MetadataTransferProducer/producer/info",
                Map.class);
        ret.put("producer-transitive-metadata", producerMetadata);

        // Get Custom Metadata From Context
        MetadataContext context = MetadataContextHolder.get();
        Map<String, String> callerTransitiveMetadata = context.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
        ret.put("consumer-transitive-metadata", callerTransitiveMetadata);
        ret.put("consumer-metadata-contents", metadataLocalProperties.getContent());

        return ret;
    }

    /**
     * health check.
     *
     * @return health check info
     */
    @GetMapping("/healthCheck")
    public String healthCheck() {
        return "pk ok";
    }
}
